(function () {
    app.controller("ClientOrderController", ['$scope', '$rootScope', 'clientOrderService', function ($scope, $rootScope, clientOrderService) {

        $scope.gridOptions = kendo.factory.getGridOptions(utils.url("clientOrder/search"), "clOrderId", [{
            field: "clientOrderNo",
            title: "客户单号",
            template: "<a href='javascript: void(0)\' ng-click='update(\"#:clOrderId#\")'>#:clientOrderNo#</a>",
            width: 140
        }, {
            field: "projectName",
            title: "项目",
            sortable: false,
            width: 140
        }, {
            field: "orderDate",
            title: "订单日期",
            width: 140
        }, {
            field: "orderType",
            title: "订单类型",
            width: 120,
            sortable: false,
            values: orderCategoryStr
        }, {
            field: "transportType",
            title: "运输方式",
            width: 120,
            sortable: false,
            values: transportTypeStr
        }, {
            field: "handoverType",
            title: "交接方式",
            width: 120,
            sortable: false,
            values: handoverTypeStr
        }, {
            field: "calculateType",
            title: "计费单位",
            sortable: false,
            values: calculateTypeStr,
            width: 120
        }, {
            field: "status",
            title: "状态",
            sortable: false,
            values: clientOrderStatusStr,
            width: 120
        }], function () {
            return {query: $scope.filterModel};
        });

        $scope.projectOptions = kendo.factory.getComboxOptions("/project/findByPermission", "projectId",
            {

            });
        $scope.goodsCategoryOptions = {
            dataSource: []
        };

        $scope.filterModel = {};
        $rootScope.reloadGrid = function () {
            $scope.gridClientOrder.dataSource.read();
        };
        $scope.update = function (userId) {
            $rootScope.$broadcast("updateClientOrder", userId);
        };
        $scope.createWindow = function () {
            $rootScope.$broadcast("createClientOrder", "");
        };
        $scope.importWindow = function () {
            $rootScope.$broadcast("importClientOrder", "");
        };
        $scope.upload = function () {
            var selectedIds = $scope.gridClientOrder.getSelectedId();
            if(selectedIds.length!=1){
                kendo.factory.notifyError("请选择一个订单");
                return;
            }
            $rootScope.$broadcast('uploadFile',selectedIds[0]);
        };

        $scope.cancel = function () {
            var orderIds = [];
            var selectData = $scope.gridClientOrder.getSelectedData();
            for (var i = 0; i < selectData.length; i++) {
                var item = selectData[i];
                orderIds.push(item.clOrderId);
                if (item.status != "CREATED") {
                    kendo.factory.notifyError("请选择待接单的订单");
                    return;
                }
            }
            if (orderIds.length < 1) {
                kendo.factory.notifyError("请选择待接单的订单！");
                return;
            }
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: contextPath + "/clientOrder/cancel",
                data: JSON.stringify(orderIds)
            }).done(function (result) {
                if (result.success) {
                    $rootScope.reloadGrid();
                    kendo.factory.notifySuccess("取消成功");
                } else {
                    kendo.factory.notifyError("取消失败" + result.message);
                }
            });
        };
    }]);
})();

